/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package servlet;

import dao.EventDao;
import dao.EventDaoImpl;
import dao.InvitedDao;
import dao.InvitedDaoImpl;
import dao.NotificationDao;
import dao.NotificationDaoImpl;
import dao.UserDao;
import dao.UserDaoImpl;
import entities1.Event;
import entities1.Invited;
import entities1.Notification;
import entities1.Users;
import java.io.IOException;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author takamflore
 */
public class UserServlet extends HttpServlet {
  private static final long serialVersionUID = 1L;
    String forward="";
    @EJB
    UserDao userDao;
    EventDao eventDao;
    InvitedDao invitation;
    NotificationDao notification;
    HttpServletRequest session;
    List<Event> events;
    List<Invited> invitations;
    List<Notification> notifications;
 
    public UserServlet(){
        super();
        userDao= new UserDaoImpl();
        eventDao= new EventDaoImpl();
        invitation= new InvitedDaoImpl();
        notification= new NotificationDaoImpl();
    }
   
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        String action= request.getParameter("action");
        Users user;
        int i= userDao.getUserId(request.getRemoteUser());
        user=userDao.getUser(i);
        if(action.equalsIgnoreCase("Profile")){
            request.setAttribute("user", user);
            forward="/WEB-INF/entity/user/View_User.jsp";
        }else if(action.equalsIgnoreCase("InvitationRequest")){
            invitations=invitation.getAllMyInvitation(user.getId());
            request.setAttribute("invitations", invitations);
            forward="/WEB-INF/entity/invitation/List_Invitation";
        }else if(action.equalsIgnoreCase("Event")){
            events= eventDao.getAllEvent(user.getId());
            request.setAttribute("events", events);
            forward="/WEB-INF/entity/event/list_event";
        }else if(action.equalsIgnoreCase("Notification")){
            notifications=notification.allNotificationUser(user.getId());
            request.setAttribute("notifications",notifications);
            forward="/WEB-INF/entity/notification/List_Notification";
        }else if(action.equalsIgnoreCase("Update")){
             forward="/WEB-INF/entity/user/View_User.jsp";
        }else{
            request.logout();
            forward="/Login.jsp";
        }
         RequestDispatcher view = request.getRequestDispatcher(forward);
        view.forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       Users user= new Users();
       user.setUserName(request.getParameter("userName"));
       user.setEmail(request.getParameter("email"));
       user.setGender(request.getParameter("gender"));
       user.setPassword(request.getParameter("password"));
       
       String userid=session.getSession().getId();
       if(userid==null ||userid.isEmpty()){
           userDao.addUser(user);
       }else{
           user.setId(Integer.parseInt(userid));
           userDao.updateUserProfile(user);
       }
        RequestDispatcher view = request.getRequestDispatcher("/WEB-INF/entity/user/View_User.jsp");
        request.setAttribute("user",userDao.getUser(user.getId()));
        view.forward(request,response);
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
